package cn.iocoder.yudao.module.infra.prometheus;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class LLMRequestMetrics {
    private Counter processingCount;

    @Resource
    private MeterRegistry registry;

    public LLMRequestMetrics() {
    }

    @PostConstruct
    public void init() {
        processingCount = Counter.builder("llm.request.processing")
                .description("LLM正在处理的请求总数")
                .tag("protocol", "http")  // 可选：添加标签区分协议类型
                .tag("provider", "ollama")  // 可选：添加标签区分协议类型
                .register(registry);
    }

    public void recordProcessingRequest() {
        processingCount.increment();
    }

    public void recordSuccessRequest() {
        recordCompleteRequest("success");
    }

    // 增加异常消息总数
    public void recordExceptionRequest() {
        recordCompleteRequest("error");
    }

    private void recordCompleteRequest(String status) {
        final Counter completeCount = Counter.builder("llm.request.complete")
                .description("LLM完成的请求总数")
                .tag("protocol", "http")  // 可选：添加标签区分协议类型
                .tag("provider", "ollama")  // 可选：添加标签区分协议类型
                .tag("status", status)
                .register(registry);
        completeCount.increment();
    }

}
